### START EXAMPLE
library("lattice")
library('RColorBrewer')
model.run = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12)

model.dataset = c(2, 1, 2, 1, 2, 1, 2, 1, 2, 2, 2, 1, 2, 1, 2, 1, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2)

model.conditional.var = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 66, 66, 66, 66, 33, 33, 33, 33, 33, 33, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 99, 99,99, 99, 66, 66, 66, 66, 66, 66, 99, 99, 99, 99, 66, 66, 66, 66, 66, 66, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99)

model.x = c(0, 0, 1, 1, 2, 2, 3, 3, 4, 5, 0, 0, 1, 1, 2, 2, 3, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 0, 0, 1, 1, 2, 2, 3, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 0, 1, 2, 3, 4, 5, 0, 1,2, 3, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5)

model.y = c(50, 50, 50, 51, 50, 55, 50, 59, 49, 46, 50, 50, 49, 53, 51, 55, 53, 54, 53, 52, 50, 50, 52, 52, 52, 46, 83, 82, 83, 87, 83, 83, 85, 89, 85, 93, 89, 97, 92, 92, 83, 85, 85, 88, 90, 90, 116, 117, 115, 117, 83, 86, 88, 91, 91, 96, 116, 116, 118, 123, 116, 118, 121,122, 120, 121, 149, 151, 154, 158, 116, 118, 120, 123, 128, 131, 149, 148, 150, 153, 116, 116, 118, 118, 122, 126, 149, 147, 145, 149, 157, 163, 149, 156, 165, 175, 180, 180, 149, 150, 152, 157, 165, 169)

dat = data.frame (run =  model.run,  dataset = model.dataset, conditional.var = model.conditional.var, x = model.x, y = model.y)
trellis.par.set(legend.bbox = "full")

#a = xyplot(y ~ x | conditional.var,
#        main = "Correctly grouped colors",
#        sub =  "I would like to group multipe data sets with the colors shown here, but I don't want the data points to be grouped in a single lines",
#        groups = dataset, # Grouping by Data Set
#        ylab =  NULL,
#        data = dat,
#        type = "l",
#        auto.key = list(x = 1, y = 1, corner = c(1, 1)),
#        #auto.key = list(space = "right")
#        )
#print(a)
#
#dev.new()
#b = xyplot(y ~ x | conditional.var,
#        main = "Correctly grouped lines",
#        sub = "I would like group two data sets with the lines shown here, but not with an individual color for each line",
#        data = dat,
#        groups = run,    # Grouping by Run
#        ylab =  NULL,
#        type = "l",
#        auto.key = list(space = "right"))
#print(b)
#
#dev.new()

n = 2

myColors = NULL
#myColors = c("red", "blue")

if (n == 1)
{ myColors <<- brewer.pal(3,"Set1")[1] }
if (n == 2)
{ myColors <<- brewer.pal(3,"Set1")[1:2] }
if (n < 9 && n > 2 )
{ myColors <<- brewer.pal(n,"Set1") }

print(myColors)

c = xyplot(y ~ x | factor(conditional.var),
      data = dat,
      groups = interaction(dataset, run),
      col = myColors,
      ylab =  NULL,
      type = "l",
      key = list(text = list(sapply(seq(n),as.character)),
                 lines = list(col = myColors),
                 space = "right"))
print(c)

### END EXAMPLE